﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using VideoClassLibrary;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class UpdateTeacher : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TeacherModule ta = new TeacherModule();
           V_teacher teacher=new V_teacher();
           int ID = Convert.ToInt32(Request.QueryString["TID"].ToString());
           teacher = ta.GetTeacherModelById(ID);
           realname.Text=teacher.VT_realname;
           dlschool.SelectedValue = teacher.SchoolID.ToString();
          txttelephone.Text=teacher.VT_mobile;
            txtemail.Text=teacher.VT_email;
           Txtdesc.Value=teacher.VT_desc;
           this.chkIsEnable.Checked = teacher.Vt_IsEnable == 1 ? true : false;
            List<V_School> vs = new List<V_School>();
            vs = ta.GetSchool();
            dlschool.DataSource = vs;
            dlschool.DataBind();
            
            if (this.CurrentUser.SchoolID == 0)
            {
            }
            else
            {
                dlschool.SelectedValue = this.dlschool.ToString();
                dlschool.Enabled = false;
            }
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        TeacherModule ta = new TeacherModule();
        V_teacher teacher = new V_teacher();
        teacher.update_time = DateTime.Now;
        teacher.SchoolID = Convert.ToInt32(dlschool.SelectedValue);
        teacher.VT_realname = realname.Text.Trim();
        teacher.VT_mobile = txttelephone.Text.Trim();
        teacher.VT_email = txtemail.Text.Trim();
        teacher.VT_desc = Txtdesc.Value.ToString();
        teacher.ID = Convert.ToInt32(Request.QueryString["TID"]);
        teacher.Vt_Image = string.Empty;
        teacher.Vt_IsEnable = this.chkIsEnable.Checked ? 1 : 0;

        string fileExtension = string.Empty;
        if (!string.IsNullOrEmpty(this.fuTeacherImage.FileName))//修改图片
        {
            fileExtension = System.IO.Path.GetExtension(fuTeacherImage.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
            string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
            dictionarySavedPath += "\\Images\\SchoolId\\TeacherId\\";
            dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", this.SchoolID.ToString()).Replace("TeacherId", teacher.ID.ToString());
            if (!Directory.Exists(dictionarySavedPath))
            {
                Directory.CreateDirectory(dictionarySavedPath);
            }
            // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
            //保存logo的文件夹绝对路径

            string logfileSavedPath = dictionarySavedPath + "photo" + fileExtension;
            //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
            this.fuTeacherImage.SaveAs(logfileSavedPath);
            teacher.Vt_Image = ("/Images/SchoolId/TeacherId/photo" + fileExtension).Replace("SchoolId", this.SchoolID.ToString()).Replace("TeacherId", teacher.ID.ToString());
            ta.UpdateTeacher(teacher.ID, teacher.Vt_Image);
        }

        int i = ta.UpdateTeacher(teacher);
        if (i == 1)
        {
            PageHelper.AlterMsgAndRedirect("修改成功！", "SearchAndDelTeacher.aspx");
        }
        else
        {
            lblMess.Text = "修改失败";
        }
    }
    //protected void renew_Click(object sender, EventArgs e)
    //{
    //    realname.Text = null;
    //    txtemail.Text = null;
    //    txttelephone.Text = null;
    //    Txtdesc.Value = null;
    //}
}
